package com.cskaoyan.controller.admin.systemcontroller;

import com.cskaoyan.bean.systembean.Role;
import com.cskaoyan.bo.systembo.PermissionBo;
import com.cskaoyan.service.systemservice.RoleService;
import com.cskaoyan.vo.BaseRespVo;
import com.cskaoyan.vo.systemvo.RoleListVo;
import com.cskaoyan.vo.systemvo.RoleOptionsVo;
import com.cskaoyan.vo.systemvo.RolePermissionPostVo;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/***
 * author:zhw
 * date:5.10
 */
@RestController
@RequestMapping("admin/role/")
public class RoleController {

    @Autowired
    RoleService roleService;

    /**
     * 返回所有角色
     *
     * @return
     */
    @RequestMapping("options")
    @RequiresPermissions("admin:role:read")
    public BaseRespVo options() {
        List<RoleOptionsVo> roleOptionsVos = roleService.options();
        return BaseRespVo.ok(roleOptionsVos);


    }

    /**
     * 按条件返回角色
     *
     * @param page
     * @param limit
     * @param name
     * @param sort
     * @param order
     * @return
     */
    @RequiresPermissions("admin:role:list")
    @RequestMapping("list")
    public BaseRespVo list(Integer page, Integer limit, String name, String sort, String order) {
        RoleListVo roleListVo = roleService.list(page, limit, name, sort, order);
        return BaseRespVo.ok(roleListVo);

    }

    /**
     * 增加
     *
     * @param role
     * @return
     */
    @RequiresPermissions("admin:role:create")
    @RequestMapping("create")
    public BaseRespVo create(@RequestBody Role role) {
        Role role1 = roleService.create(role);
        return BaseRespVo.ok(role1);

    }

    @RequiresPermissions("admin:role:update")
    @RequestMapping("update")
    public BaseRespVo update(@RequestBody Role role) {
        roleService.update(role);
        return BaseRespVo.ok();

    }

    /**
     * 删
     *
     * @param role
     * @return
     */
    @RequiresPermissions("admin:role:delete")
    @RequestMapping("delete")
    public BaseRespVo delete(@RequestBody Role role) {
        roleService.delete(role);
        return BaseRespVo.ok();

    }

    @RequiresPermissions("admin:role:permission:get")
    @RequestMapping("permissions")
    public BaseRespVo permissions1(Integer roleId) {
        RolePermissionPostVo rolePermissionPostVo = roleService.permissions1(roleId);
        return BaseRespVo.ok(rolePermissionPostVo);

    }

    @RequiresPermissions("admin:role:permission:update")
    @PostMapping("permissions")
    public BaseRespVo permissions2(@RequestBody PermissionBo permissionBo) {
        roleService.permissions2(permissionBo);
        return BaseRespVo.ok();


    }


}
